<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../js/vue.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;

    }

    .nav {
        display: flex;
        justify-content: center;
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 40px;
    }

    .item {
        flex: 1;
        line-height: 40px;
        background-color: #000;
        margin-right: 1px;
        color: #fff;
        text-align: center;
        cursor: pointer;
    }

    .item a {
        display: block;
        width: 100%;
        height: 100%;
        color: inherit;
        text-decoration: none;
    }

    .active {
        background-color: tomato;
    }

    .content {
        text-align: center;
    }
</style>

<body>
    <div id="app">
        <div class="nav">
            <div class="item">
                <router-link to="/">首页</router-link>
            </div>
            <div class="item">
                <router-link to="/about">关于</router-link>
            </div>
            <div class="item">
                <router-link to="/list">列表</router-link>
            </div>
            <div class="item">
                <router-link to="/mine">我的</router-link>
            </div>
        </div>
        <router-view />
    </div>
</body>
<template id="home">
    <div class="content">
        首页
    </div>
</template>
<template id="about">
    <div class="content">
        关于
    </div>
</template>
<template id="list">
    <div class="content">
        列表
    </div>
</template>
<template id="mine">
    <div class="content">
        我的
    </div>
</template>
<template id="notfond">
    <div class="content">
        404
    </div>
</template>
<template id="routerLink">
    <a :href="'#'+to">
        <slot></slot>
    </a>
</template>
<template id="routerView">
    <div>
        <component :is="com"></component>
    </div>
</template>

<script>
    let home = {
        template: "#home",
    }
    let about = {
        template: "#about",
    }
    let list = {
        template: "#list",
    }
    let mine = {
        template: "#mine",
    }
    let routerLink = {
        props: ["to"],
        template: "#routerLink",
    }
    let notfond = {
        template: "#notfond"
    }

    let routerView = {
        template: "#routerView",
        data() {
            return {
                com: "home",
                activehash: "#/",
                list: [
                    {id: 0, com: "notfond", path: "/notfond"},
                    {id: 1, com: "home", path: "/"},
                    {id: 2, com: "about", path: "/about"},
                    {id: 3, com: "list", path: "/list"},
                    {id: 4, com: "mine", path: "/mine"},
                ]
            }
        },
        components: {
            home,
            about,
            list,
            mine,
            notfond
        },
        methods: {
            changehash() {
                let hash = location.hash;//得到值 #/about  #/list  
                hash = hash.split("#/")[1];//得到没有#/的值

                //根据hash知道对应的组件
                if (hash) {
                    let list = this.list.filter(item => item.com == hash);//找到点击的这个集合
                    if (list.length) {
                        let info = list[0];
                        this.com = info.com;
                        // this.activeindex = info.id;
                    } else {
                        this.com = "notfond";
                    }
                } else {
                    this.com = "home";
                    // this.activeindex = 1;

                }
            }
        },
        mounted() {//页面结构加载完成
            this.changehash();
            window.onhashchange = () => {//hash值发生改变的事件
                this.changehash();
            }
        }
    }
    new Vue({
        el: "#app",
        components: {
            routerLink,
            routerView
        }
    })
</script>

</html>